<?php

namespace application\district\controller;

use application\admin\controller\base as auth;

class district extends auth {

    /**
     * 首页
     */
    public function index() {
        $values = array(intval($_GET['pid']), intval($_GET['cid']), intval($_GET['did']));
        $elems = array($_GET['province'], $_GET['city'], $_GET['district']);
        $level = 1;
        $upids = array(0);
        $theid = 0;
        for ($i = 0; $i < 3; $i++) {
            if (!empty($values[$i])) {
                $theid = intval($values[$i]);
                $upids[] = $theid;
                $level++;
            } else {
                for ($j = $i; $j < 3; $j++) {
                    $values[$j] = '';
                }
                break;
            }
        }
        if (submitcheck('editsubmit')) {
            $delids = array();
            foreach (db('district')->where("upid in(" . $theid . ")")->order("displayorder")->select() as $value) {
                $usetype = 0;
                if ($_POST['birthcity'][$value['id']] && $_POST['residecity'][$value['id']]) {
                    $usetype = 3;
                } elseif ($_POST['birthcity'][$value['id']]) {
                    $usetype = 1;
                } elseif ($_POST['residecity'][$value['id']]) {
                    $usetype = 2;
                }
                if (!isset($_POST['district'][$value['id']])) {
                    $delids[] = $value['id'];
                } elseif ($_POST['district'][$value['id']] != $value['name'] || $_POST['displayorder'][$value['id']] != $value['displayorder'] || $usetype != $value['usetype']) {
                    db('district')->update(array('name' => $_POST['district'][$value['id']], 'displayorder' => $_POST['displayorder'][$value['id']], 'usetype' => $usetype), array('id' => $value['id']));
                }
            }
            if ($delids) {
                $ids = $delids;
                for ($i = $level; $i < 4; $i++) {
                    $ids = array();
                    foreach (db('district')->where("upid in(" . implode(",", $delids) . ")")->order("displayorder")->select() as $value) {
                        $value['id'] = intval($value['id']);
                        $delids[] = $value['id'];
                        $ids[] = $value['id'];
                    }
                    if (empty($ids)) {
                        break;
                    }
                }
                db('district')->delete($delids);
            }
            if (!empty($_POST['districtnew'])) {
                $inserts = array();
                $displayorder = '';
                foreach ($_POST['districtnew'] as $key => $value) {
                    $displayorder = trim($_POST['districtnew_order'][$key]);
                    $value = trim($value);
                    if (!empty($value)) {
                        db('district')->insert(array('name' => $value, 'level' => $level, 'upid' => $theid, 'displayorder' => $displayorder));
                    }
                }
            }
            showmsg('修改地区数据成功', '/district/district/index?action=district&pid=' . $values[0] . '&cid=' . $values[1] . '&did=' . $values[2]);
        } else {
            $upids = is_array($upids) ? implode(",", $upids) : intval($upids);
            if ($upids !== null) {
                $items = db('district')->where("upid in(" . $upids . ")")->order("displayorder")->select();
            } else {
                $items = array();
            }
            //联动
            $options = array(1 => array(), 2 => array(), 3 => array());
            $thevalues = array();
            foreach ($items as $value) {
                $options[$value['level']][] = array($value['id'], $value['name']);
                if ($value['upid'] == $theid) {
                    $thevalues[] = array($value['id'], $value['name'], $value['displayorder'], $value['usetype']);
                }
            }
            $names = array('province', 'city', 'district');
            for ($i = 0; $i < 3; $i++) {
                $elems[$i] = !empty($elems[$i]) ? $elems[$i] : $names[$i];
            }
            $html = '';
            for ($i = 0; $i < 3; $i++) {
                $l = $i + 1;
                $jscall = ($i == 0 ? 'this.form.city.value=\'\';this.form.district.value=\'\';' : '') . "refreshdistrict('$elems[0]', '$elems[1]', '$elems[2]')";
                $html .= '<select class="form-control" name="' . $elems[$i] . '" id="' . $elems[$i] . '" onchange="' . $jscall . '">';
                $html .= '<option value="">' . L('district_level_' . $l) . '</option>';
                foreach ($options[$l] as $option) {
                    $selected = $option[0] == $values[$i] ? ' selected="selected"' : '';
                    $html .= '<option value="' . $option[0] . '"' . $selected . '>' . $option[1] . '</option>';
                }
                $html .= '</select>&nbsp;&nbsp;';
            }
            //debug($thevalues);
            $this->assign('items', $thevalues);
            $this->assign('html', $html);
            $this->assign('values', $values);

            $this->display('district_index');
        }
    }

}
